package com.uchengwang.ui.utils;

import android.os.Environment;
import android.text.TextUtils;

import java.io.File;

/**
 * Created by U城网 on 2017/7/8.
 * 存储路径
 */

public class PathUtils {
    /***
     *基本路径
     */
    private static final String UCW_BASE_PATH = Environment.getExternalStorageDirectory().toString()+"/ucwDownload";
    //下载头像保存地址
    public static final String UCW_HEAD_IMAGE = UCW_BASE_PATH + "/head_image";
    //下载普通保存图片
    public static final String UCW_COM_IMAGE = UCW_BASE_PATH + "/com_image";
    //裁剪的图片
    public static final String UCW_CROP_IMAGE = UCW_BASE_PATH + "/crop_image";

    private PathUtils(){}
    private static PathUtils utils;

    public static PathUtils getUtils() {
        if (utils == null){
            utils = new PathUtils();
        }
        return utils;
    }

    private boolean mkdirs(String path) {
        if (path == null) return false;
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }

        return true;
    }

    /**
     * 根据url,生成头像存储地址
     * */
    public String getHeadImagePath(String url) {
        if (!mkdirs(UCW_HEAD_IMAGE)) return null;
        if (TextUtils.isEmpty(url)){
            return null;
        }
        //取出后缀名
        String lastUrl = url.substring(url.lastIndexOf("."));
        //下载时间 年月日
        String time = DateUtil.getDateUtil().getDateByMillis(DateUtil.DATE_FORMAT_YMD, System.currentTimeMillis());
        return UCW_HEAD_IMAGE + "/ucw_" + time + "_" + System.currentTimeMillis() + lastUrl;
    }

    /**
     * 根据url生成普通图片存储地址
     * */
    public String getComImagePath(String url) {
        if (!mkdirs(UCW_COM_IMAGE)) return null;
        if (TextUtils.isEmpty(url)){
            return null;
        }
        //取出后缀名
        String lastUrl = url.substring(url.lastIndexOf("."));
        //下载时间 年月日
        String time = DateUtil.getDateUtil().getDateByMillis(DateUtil.DATE_FORMAT_YMD, System.currentTimeMillis());
        return UCW_COM_IMAGE + "/ucw_" + time + "_" + System.currentTimeMillis() + lastUrl;
    }

    /**
     * 根据url生成裁剪存储地址
     * */
    public String getCropImagePath(String url) {
        if (!mkdirs(UCW_CROP_IMAGE)) return null;
        if (TextUtils.isEmpty(url)){
            return null;
        }
        //取出后缀名
        String lastUrl = url.substring(url.lastIndexOf("."));
        //下载时间 年月日
        String time = DateUtil.getDateUtil().getDateByMillis(DateUtil.DATE_FORMAT_YMD, System.currentTimeMillis());
        return UCW_CROP_IMAGE + "/ucw_" + time + "_" + System.currentTimeMillis() + lastUrl;
    }


}
